import 'package:flutter/material.dart';

class AddRemindVC extends StatefulWidget {
  @override
  _AddRemindVCState createState() => new _AddRemindVCState();
}

class _AddRemindVCState extends State<AddRemindVC> {
  var _isAlert = true;
  var _isTop = false;
  String _text;

  var _nowTime = DateTime.now().millisecondsSinceEpoch;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('添加新的倒计时'),
      ),
      body: ListView(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(8),
            child: TextField(
              onChanged: (value) {
                this._text = value;
              },
              style: TextStyle(
                fontSize: 18,
                color: Colors.black,
              ),
              autofocus: true,
              maxLines: null,
              decoration: InputDecoration(hintText: "输入提醒事项"),
            ),
          ),
          ListTile(
            leading: Icon(Icons.calendar_today),
            title: Text("请选择截止时间"),
            subtitle:
//            Text(getFormattedDate(_nowTime)),
                StreamBuilder(
                    initialData: _nowTime,
                    builder: (context, snaphot) =>
                        Text(getFormattedDate(_nowTime))),
            onTap: () {
              _selectDate(context);
            },
          ),
          ListTile(
              leading: Icon(Icons.timer),
              title: Text("是否置顶"),
//              subtitle: Text("aaa"),
//              subtitle: Text("No Reminder"),
              trailing: Switch(
                  value: _isTop,
                  onChanged: (value) {
                    setState(() {
                      _isTop = value;
                    });
                  })),
          ListTile(
              leading: Icon(Icons.timer),
              title: Text("是否提醒"),
//              subtitle: Text("No Reminder"),
              trailing: Switch(
                  value: _isAlert,
                  onChanged: (value) {
                    setState(() {
                      _isAlert = value;
                    });
                  })),
        ],
      ),
      floatingActionButton: FloatingActionButton(
//        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.assignment_turned_in),
        onPressed: () {
//          Navigator.push(context, MaterialPageRoute(builder: (context) => AddRemindVC()));
//          Navigator.pop(context);
          Navigator.of(context)
              .pop({"text": this._text, "color": Colors.deepPurpleAccent});
        },
      ),
    );
  }

  String getFormattedDate(int dueDate) {
    print(dueDate);
    DateTime date = DateTime.fromMillisecondsSinceEpoch(dueDate);
    print("${date.month}=${date.day}");
    return "${date.year}年${date.month}月${date.day}日";
//    return "${[date.month - 1]}  ${date.day}";
  }

  Future<Null> _selectDate(BuildContext context) async {
//    AddTaskBloc createTaskBloc = BlocProvider.of(context);
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2015),
      lastDate: DateTime(2101),
      locale: Locale('zh'),
    );
    if (picked != null) {
//      createTaskBloc.updateDueDate(picked.millisecondsSinceEpoch);
//      print(picked.millisecondsSinceEpoch);
      print(picked);
      this._nowTime = picked.millisecondsSinceEpoch;
      setState(() {});
    }
  }
}
